﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.OpenGL;
using Silvermoon.Shapes;
using System.Drawing;
using Silvermoon.Classes;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Item which contains text.
    /// </summary>
    public class TextItem : VirtualItem
    {
        private static readonly Margin defaultPadding = new Margin(8, 4, 8, 4);
        protected StringShape label;
        private Margin padding;
        private bool autoHeight;
        private FontSprite font;

        protected StringShape Label { get { return label; } }

        public TextItem()
            : base()
        {
            label = new StringShape { Alignment = Alignment.Near, LineAlignment = Alignment.Center };
            Shapes.AddLast(label);
            Height = 80;
        }

        public bool AutoHeight
        {
            get { return autoHeight; }
            set
            {
                if (autoHeight != value)
                {
                    autoHeight = value;
                    Invalidate(InvalidationFlag.Layout);
                }
            }
        }

        public LineMode LineMode
        {
            get { return label.LineMode; }
            set
            {
                if (label.LineMode != value)
                {
                    label.LineMode = value;
                    Invalidate(InvalidationFlag.Layout);
                }
            }
        }

        public Alignment Alignment
        {
            get { return label.Alignment; }
            set
            {
                if (Alignment != value)
                {
                    label.Alignment = value;
                    Invalidate(InvalidationFlag.Layout);
                }
            }
        }

        public override Size Size
        {
            get
            {
                return AutoHeight ? new Size(base.Width, label.Height) : base.Size;
            }
            set
            {
                bool autoSize = autoHeight;
                if (autoHeight)
                {
                    label.Size = Scale(value);
                    base.Size = value;
                    label.AutoSize = autoSize;
                }
                else base.Size = value;
            }
        }


        public Margin Padding
        {
            get { return padding ?? defaultPadding; }
            set
            {
                if (!Padding.Equals(value))
                {
                    padding = value;
                    Invalidate(InvalidationFlag.LayoutAppearance);
                }
            }
        }



        public TextItem(string text)
            : this()
        {
            Text = text;
        }

        public TextItem(string text, bool enabled)
            : this()
        {
            this.Text = text;
            this.Enabled = enabled;
        }

        public TextItem(string text, FontSprite font)
            : this()
        {
            this.Text = text;
            this.Font = font;
        }

        public FontSprite Font
        {
            get { return label.Font; }
            set
            {
                if (font != value)
                {
                    font = value;
                    label.Font = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }

        public string Text
        {
            get { return label.Text; }
            set
            {
                if (Text != value)
                {
                    label.Text = value;
                    OnTextChanged();
                }
            }
        }

        protected virtual void OnTextChanged()
        {
            Invalidate(InvalidationFlag.Text);
        }


        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Layout | InvalidationFlag.Text))
            {
                label.Bounds = Padding.Layout(label.Bounds, Size);
            }
            base.OnLayout();
        }

        protected override void ApplyStyle()
        {
            base.ApplyStyle();
            if (font == null) Font = SystemFonts.Medium;
        }

    }
}
